//
//  MBProgressHUD+WL.m
//  MBProgressHUD+WL
//
//  Created by WL on 2018/5/14.
//

#import "MBProgressHUD+WL.h"

@implementation MBProgressHUD (WL)


+ (MBProgressHUD *)showText:(NSString *)text{
    return [self showText:text toView:nil];
}
+ (MBProgressHUD *)showText:(NSString *)text toView:(UIView *)view{
    if (view == nil) {
//        view = [UIApplication sharedApplication].keyWindow;
        UIViewController *vc = [self topmostViewController];
        if (!vc) {
            return nil;
        }
        NSLog(@"%@",NSStringFromClass([vc class]));
        view = vc.view;
    }
    if (!text.length) {
        return nil;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.numberOfLines = 0;
    hud.userInteractionEnabled = NO;

    hud.label.text = text;
    
    hud.label.textColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
    
//    //遮挡
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.backgroundView.color =  [UIColor colorWithWhite:0.f alpha:.2f];
    
    hud.removeFromSuperViewOnHide = YES;
    double delayTime = 1.2;
    delayTime = MAX(delayTime, text.length*0.3);
    delayTime = MIN(delayTime, 2.5);
    [hud hideAnimated:YES afterDelay:delayTime];
    return hud;
}


+ (MBProgressHUD *)showSuccess:(NSString *)success{
    return [self showSuccess:success toView:nil];
}
+ (MBProgressHUD *)showSuccess:(NSString *)success toView:(UIView *)view{
   
    NSString *fwPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:@"PRBaseDependTool.framework"];
    NSBundle *bundle = [NSBundle bundleWithPath:fwPath];
    NSString *path = [bundle pathForResource:@"MBProgressHUD" ofType:@"bundle"];
    bundle = [NSBundle bundleWithPath:path];
    NSString *imagePath = [bundle pathForResource:@"success@2x" ofType:@"png"];
    
    UIImage *image = [[UIImage imageWithContentsOfFile:imagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return [self showText:success icon:image toView:view];
}


+ (MBProgressHUD *)showError:(NSString *)error{
    return [self showError:error toView:nil];
}
+ (MBProgressHUD *)showError:(NSString *)error toView:(UIView *)view{
    
    NSString *fwPath = [[[NSBundle mainBundle] privateFrameworksPath] stringByAppendingPathComponent:@"PRBaseDependTool.framework"];
    NSBundle *bundle = [NSBundle bundleWithPath:fwPath];
    NSString *path = [bundle pathForResource:@"MBProgressHUD" ofType:@"bundle"];
    bundle = [NSBundle bundleWithPath:path];
    NSString *imagePath = [bundle pathForResource:@"error@2x" ofType:@"png"];
    
    UIImage *image = [[UIImage imageWithContentsOfFile:imagePath] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return [self showText:error icon:image toView:view];
}


+ (MBProgressHUD *)showText:(NSString *)text icon:(UIImage *)icon toView:(UIView *)view{
    
    if (!icon) {
        //图片不存在情况，特殊处理
        return [self showText:text toView:view];
    }
    
    
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    
    hud.mode = MBProgressHUDModeCustomView;
    
    hud.customView = [[UIImageView alloc] initWithImage:[icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
//    hud.square = YES;
 
    hud.detailsLabel.text = text;
    hud.detailsLabel.font = [UIFont systemFontOfSize:14];
    
    hud.detailsLabel.textColor = [UIColor whiteColor];
    
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];

    
//    //遮挡
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.backgroundView.color =  [UIColor colorWithWhite:0.f alpha:.2f];
    
    hud.removeFromSuperViewOnHide = YES;
    double delayTime = 1.2;
    delayTime = MAX(delayTime, text.length*0.3);
    delayTime = MIN(delayTime, 2.5);
    [hud hideAnimated:YES afterDelay:delayTime];
    return hud;
}


+ (MBProgressHUD *)showHUD{
    return [self showMessage:nil];
}
+ (MBProgressHUD *)showMessage:(NSString *)message{
    return [self showMessage:message toView:nil];
}
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor whiteColor];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
//    hud.square = YES;
    hud.label.text = message;
    
    hud.label.textColor = [UIColor whiteColor];
    hud.bezelView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.75];
    
    
    hud.removeFromSuperViewOnHide = YES;
    
//    //遮挡
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.backgroundView.color =  [UIColor colorWithWhite:0.f alpha:.2f];
    
    return hud;
}


+ (BOOL)hideHUDForView:(UIView *)view{
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    return [self hideHUDForView:view animated:YES];
}
+ (BOOL)hideHUD{
    return [self hideHUDForView:nil];
}

+ (void)hideAllHUDForView:(UIView *)view {
    if (view == nil) {
        view = [UIApplication sharedApplication].keyWindow;
    }
    
    for (MBProgressHUD *hud in [self AllHUDForView:view]) {
        if (hud != nil) {
            hud.removeFromSuperViewOnHide = YES;
            [hud hideAnimated:YES];
        }
    }
}
+ (void)hideAllHUD {
    [self hideAllHUDForView:nil];
}


+ (NSArray <MBProgressHUD *> *)AllHUDForView:(UIView *)view {
    
    NSMutableArray *HUDArr = [NSMutableArray array];
    
    NSEnumerator *subviewsEnum = [view.subviews reverseObjectEnumerator];
    for (UIView *subview in subviewsEnum) {
        if ([subview isKindOfClass:self]) {
            MBProgressHUD *hud = (MBProgressHUD *)subview;
            [HUDArr addObject:hud];
        }
    }
    return [HUDArr copy];
}

//获取当前控制器
+ (nullable UIViewController *)topmostViewController {
    //rootViewController需要是TabBarController,排除正在显示FirstPage的情况
    UIViewController *rootViewContoller = [UIApplication sharedApplication].delegate.window.rootViewController;
    if (!rootViewContoller) {
        return nil;
    }
    //当前显示哪个tab页
    UINavigationController *rootNavController = nil;
    if([rootViewContoller isKindOfClass:[UITabBarController class]]){
        rootNavController = (UINavigationController *) [(UITabBarController*)rootViewContoller selectedViewController];
    }else if([rootViewContoller isKindOfClass:[UINavigationController class]]){
        rootNavController = (UINavigationController *)rootViewContoller;
    } else {
        return rootViewContoller;
    }
    
    if (!rootNavController) {
        return nil;
    }
    
    UINavigationController *navController = rootNavController;
    while ([navController isKindOfClass:[UINavigationController class]]) {
        UIViewController *topViewController = [navController topViewController];
        if ([topViewController isKindOfClass:[UINavigationController class]]) { //顶层是个导航控制器，继续循环
            navController = (UINavigationController *) topViewController;
        } else {
            //是否有弹出presentViewControllr;
            UIViewController *presentedViewController = topViewController.presentedViewController;
            while (presentedViewController) {
                topViewController = presentedViewController;
                if ([topViewController isKindOfClass:[UINavigationController class]]) {
                    break;
                } else {
                    presentedViewController = topViewController.presentedViewController;
                }
            }
            navController = (UINavigationController *) topViewController;
        }
    }
    return (UIViewController *) navController;
}

@end
